#ifndef IO_FILTER_CHAIN_H
#define IO_FILTER_CHAIN_H
#include "io_filter.h"
#include <vector>
namespace vpm
{
	namespace mina
	{

		class IoFilterChain  : public IoFilter
		{
		public:
			typedef std::vector<IoFilterPtr > FilterChainVec;

			virtual ~IoFilterChain()
			{
			}

            inline void set_head_filter(IoFilterPtr & filter)
            {
                m_filterChain.insert(m_filterChain.begin(),filter); 
            }

            inline void set_tail_filter(IoFilterPtr & filter)
            {
                m_filterChain.push_back(filter); 
            }

			void append_parallel_filter(IoFilterPtr & filter)
			{
				filter->filter_type(filter->filter_type());//TODO check it 
				m_filterChain.push_back(filter);
			}

            void append_serial_filter(IoFilterPtr & filter)
            {
                IoFilterPtr &lastFilter = m_filterChain.back();
                if (lastFilter)
                {
                    lastFilter->next_filter(filter); 
                }
            }

			virtual FilterAction process(MessageContext & msg)
			{
				FilterAction action;
				for (size_t position = 0; position < m_filterChain.size(); ++position)
				{	
					action = m_filterChain[position]->process(msg);

					switch (action)
					{
					case FILTER_SKIP_ALL_CHAINS:
						return FILTER_SKIP_ALL_CHAINS; 

					case FILTER_WAIT_EVENT:
						return FILTER_WAIT_EVENT; 
					case FILTER_SKIP_THIS_CHAIN:
						return FILTER_CONTINUE; 
					default:
						;
					}
			
				}
				return FILTER_CONTINUE;
			}

		private:
			FilterChainVec m_filterChain; 
		};
	}
}


#endif // IO_FILTER_CHAIN_H
